package com.jieyang.oa.controller.income;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.Vo.ExpenseListVo;
import com.jieyang.oa.pojo.Vo.IncomeListVo;
import com.jieyang.oa.pojo.entity.Employee;
import com.jieyang.oa.pojo.entity.Income;
import com.jieyang.oa.service.IncomeService;
import com.jieyang.oa.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 29276
 * @version 1.0
 * @description: 收支管理
 * @date 2021/8/23 21:50
 */
@WebServlet("/incomeServlet")
public class IncomeServlet extends BaseServlet {

    @Autowired
    private IncomeService incomeService;

    @Autowired
    private PaymentService paymentService;

    /**
     * @Author 2927624517@qq.com
     * @Description 添加收入
     * @Date 2021/8/28 15:03
     * @Param [req, resp]
     * @return void
    **/
    public void incomeAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");
        Long deptNo = Long.valueOf(req.getParameter("deptNo"));
        Long amount = Long.valueOf(req.getParameter("amount"));
        String incomeType = req.getParameter("incomeType");
        String incomeDate = req.getParameter("incomeDate");
        String incomeDesc = req.getParameter("incomeDesc");
        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();
        Income income = new Income();
        income.setDeptNo(deptNo);
        income.setAmount(amount);
        income.setIncomeDate(incomeDate);
        income.setIncomeDesc(incomeDesc);
        income.setIncomeType(incomeType);
        income.setEmpId(empId);

        Integer insert = incomeService.insert(income);
        if (insert == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询收入类型
     * @Date 2021/8/28 15:04
     * @Param [req, resp]
     * @return void
    **/
    public void incomeTypeList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        List<Income> incomeTypes = incomeService.selectIncomeTypeList();
        resp.getWriter().write(new Gson().toJson(incomeTypes));
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询收入列表
     * @Date 2021/8/28 15:06
     * @Param [req, resp]
     * @return void
    **/
    public void incomeList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        String startDate = req.getParameter("startDate");
        String endDate = req.getParameter("endDate");
        String incomeType = req.getParameter("incomeType");

        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        PageInfo<IncomeListVo> IncomeListVoPageInfo = incomeService.selectAll(pageNum, pageSize, startDate, endDate, incomeType);

        resp.getWriter().write(new Gson().toJson(IncomeListVoPageInfo));

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 删除收入记录
     * @Date 2021/8/28 15:08
     * @Param [req, resp]
     * @return void
    **/
    public void deleteIncome(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Long incomeId = Long.valueOf(req.getParameter("incomeId"));
        Integer delete = incomeService.delete(incomeId);
        if (delete == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询一条收入记录
     * @Date 2021/8/28 15:08
     * @Param [req, resp]
     * @return void
    **/
    public void selectIncome(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        resp.setContentType("text/html;charset=utf-8");
        Long incomeId = Long.valueOf(req.getParameter("incomeId"));

        IncomeListVo incomeListVo = incomeService.selectByIncomeId(incomeId);

        req.setAttribute("income", incomeListVo);
        req.getRequestDispatcher("income/incomeInfo.jsp").forward(req, resp);

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询支出列表
     * @Date 2021/8/28 15:09
     * @Param [req, resp]
     * @return void
    **/
    public void expenseList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        resp.setContentType("text/html;charset=utf-8");
        String startDate = req.getParameter("startDate");
        String endDate = req.getParameter("endDate");

        Long deptNo;
        String no = req.getParameter("deptNo");
        if (no != null) {
            if ("".equals(no)) {
                deptNo = null;
            } else {
                deptNo = Long.valueOf(no);
            }

        } else {
            deptNo = null;
        }
        String realName = req.getParameter("realName");

        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        PageInfo<ExpenseListVo> expenseListVoPageInfo = paymentService.selectExpense(startDate, endDate, deptNo, realName, pageNum, pageSize);

        resp.getWriter().write(new Gson().toJson(expenseListVoPageInfo));

    }
}
